home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_56 / filldma.asm < prev    next >
Assembly Source File  |  1995-01-01  |  8KB  |  271 lines

  1. ; FILLDMA.ASM - library for playing in normal mode (not pollplay)
  2.  
  3. model large,pascal
  4.  
  5. INCLUDE BORDER.INC
  6. .data
  7. EXTRN EndOfSong   : BYTE          ; Flag if we reach the end of the Song :(
  8. EXTRN DMAhalf     : BYTE          ; last part of DMAbuffer we have to fill
  9. EXTRN lastready   : BYTE          ; last part of DMAbuffer we calculated last call
  10. EXTRN NumBuffers  : BYTE
  11. EXTRN BPT         : WORD          ; Bytes Per Tick - depends on tempo
  12. EXTRN _16Bit      : BYTE
  13. EXTRN TICKBUFFER  : DWORD
  14. EXTRN DMAbuffer   : DWORD
  15. EXTRN toslow      : BYTE
  16. EXTRN stereo      : BYTE
  17. EXTRN JustInFill  : BYTE
  18. EXTRN DMArealbufsize:WORD
  19. EXTRN savhandle:word
  20. EXTRN useEMS   :byte
  21. EXTRN LQmode   :byte
  22.       errorsav   DB ?
  23. ends
  24.  
  25. .code
  26. .386
  27.  
  28. public fill_dmabuffer
  29. public mixroutines
  30. EXTRN calc_stereo_tick
  31. EXTRN calc_mono_tick
  32.  
  33. mixroutines PROC NEAR
  34.             cmp       [stereo],0
  35.             je        nostereo
  36.             call near ptr calc_stereo_tick
  37.             ret
  38. nostereo:   call near ptr calc_mono_tick
  39.             ret
  40. mixroutines ENDP
  41.  
  42. convert_8  PROC NEAR
  43.            ; IN:   CX - count of values to convert
  44.            ;       ES:DI - pointer to DMAbuffer
  45.            ;       FS:SI - pointer to tickbuffer
  46.            ;       DS:?? - pointer to posttable
  47.            ; Result: a 16bit to 8bit convert of CX values
  48.            ; OUT:
  49.            ;      CX = 0
  50.            ;      BX = OLD_SI+2*OLD_CX
  51.            ;      SI = OLD_SI
  52.            ;      DI = OLD_DI+OLD_CX
  53.            ;      AL ???
  54.            ;
  55.            mov     bx,si
  56. conv_loop: mov     si,fs:[bx]
  57.            add     bx,2
  58.            mov     al,ds:[si]
  59.            mov     es:[di],al
  60.            inc     di
  61.            dec     cx
  62.            jnz     conv_loop
  63.            ret
  64. convert_8  ENDP
  65.  
  66. LQconvert_8  PROC NEAR
  67.              ; IN:   CX - count of values to convert
  68.              ;       ES:DI - pointer to DMAbuffer
  69.              ;       FS:SI - pointer to tickbuffer
  70.              ;       DS:?? - pointer to posttable
  71.              ; Result: a 16bit to 8bit convert of CX values
  72.              ; OUT:
  73.              ;      CX = 0
  74.              ;      BX = OLD_SI+2*OLD_CX
  75.              ;      SI = OLD_SI
  76.              ;      DI = OLD_DI+OLD_CX
  77.              ;      AL ???
  78.              ;
  79.              cmp     [LQmode],0
  80.              je      convert_8
  81.              shl     di,1
  82.              mov     bx,si
  83.              cmp     [stereo],1
  84.              je      st_cv
  85. LQc_loop:    mov     si,fs:[bx]
  86.              add     bx,2
  87.              mov     al,ds:[si]
  88.              mov     ah,al
  89.              mov     es:[di],ax
  90.              add     di,2
  91.              dec     cx
  92.              jnz     LQc_loop
  93.              ret
  94. st_cv:       shr     cx,1
  95.  
  96. st_cvl:      mov     si,fs:[bx]
  97.              add     bx,2
  98.              mov     al,ds:[si]
  99.              mov     si,fs:[bx]
  100.              add     bx,2
  101.              mov     ah,ds:[si]
  102.              mov     es:[di],ax
  103.              add     di,2
  104.              mov     es:[di],ax
  105.              add     di,2
  106.              dec     cx
  107.              jnz     st_cvl
  108.              ret
  109. LQconvert_8  ENDP
  110.  
  111. ; ---------------------------------------------------------------------------
  112. ; PROC fill_DMABuffer  .... called by IRQ (generated by SB)
  113. ; IN: nothing      OUT: nothing (but crap :)
  114. ; WHAT TO DO:
  115. ; Convert DMArealBufsize[1] words from tickbuffer into playbuffer (position
  116. ; spezified with DMAhalf). If not enough data is left in tickbuffer call
  117. ; 'calcroutines' for next tick (calcroutines also do 'note' handling).
  118. ; ---------------------------------------------------------------------------
  119. fill_DMAbuffer PROC NEAR
  120.              cmp  [_16bit],0
  121.              jne  is16bit
  122. again8:      call fill_8bit
  123.              mov  al,[lastready]
  124.              cmp  al,[DMAhalf]
  125.              jne  again8
  126.              ret
  127. is16bit:     ; do nothing :) ... is not written yet !
  128.              ret
  129. fill_DMAbuffer ENDP
  130.  
  131. fill_8bit PROC NEAR  ; New routine (faster ?)
  132.           ; save these values (we are in an interrupt !)
  133.  
  134.           push    eax ebx ecx edx ebp esi edi ds es fs gs
  135.  
  136.           ; check if we are allready in calculation routines
  137.           ; if we are the PC is to slow -> how you wanna handle it ?
  138.           cmp     [justinfill],1
  139.           jne     noproblem
  140.           mov     cx,0ffffh
  141. waitfor:  mov     ax,cx
  142.           shl     ax,16
  143.           shr     ax,16
  144.           cmp     [justinfill],1
  145.           loopz   waitfor
  146.           jnz     slow
  147. noproblem:
  148.           setborder  7
  149.           ; for check if to slow set a variable (flag that we are allready in calc)
  150.           mov     [justinfill],1
  151.  
  152.           cmp     [EndOfSong],0
  153.           jne     Song_ends
  154.  
  155.           ; before calling mixroutines :
  156.           ; save EMS mapping !
  157.           cmp     [useEMS],0
  158.           je      donotsave
  159.           call near ptr SAVE_MAPPING
  160. donotsave:
  161.  
  162.           setborder    1
  163.           call    near ptr mixroutines              ; calc 'dmarealbufsize' bytes into the tickbuffer
  164.  
  165.           setborder    7
  166.           ; now restore EMS mapping:
  167.           cmp     [useEMS],0
  168.           je      donotrestore
  169.           call near ptr RESTORE_MAPPING
  170. donotrestore:
  171.  
  172.           mov     ax,word ptr [tickbuffer+2]
  173.           mov     fs,ax                            ; FS is tickbuffer segment
  174.  
  175.           xor     si,si                            ; start in tickbuffer
  176.  
  177.           mov     ax,word ptr [DMAbuffer+2]
  178.           mov     es,ax                            ; ES is playbuffer segment
  179.  
  180.           mov     ah,[numbuffers]
  181.           dec     ah
  182.           mov     al,[lastready]
  183.           inc     al
  184.           and     al,ah
  185.           mov     [lastready],al
  186.           xor     ah,ah
  187.           mov     di,ax
  188.           shl     di,1
  189.           mov     di,[dmarealbufsize+di]           ; startoffset in DMAbuffer
  190.  
  191.           mov     cx,[DMArealBufsize+2]            ; CX = number of bytes in tickbuffer
  192.  
  193.           call    LQconvert_8
  194.  
  195. outside:
  196.           mov     [justinfill],0
  197. endoffill:
  198.           setborder 0
  199.           pop     gs fs es ds edi esi ebp edx ecx ebx eax
  200.           ret
  201.           ; bye bye C ya later
  202.  
  203.  
  204. slow:     ; sorry your PC is to slow - maincode may ignore this flag
  205.           ; but it'll sound ugly :(
  206.  
  207.           mov     [toslow],1
  208.  
  209.           ; simply fill the half with last correct mixed value
  210.           mov     cx,[dmarealBufsize+2]              ; we have to calc. dmarealBufsize bytes
  211.           mov     ax,word ptr [DMAbuffer+2]
  212.           mov     es,ax                            ; ES is playbuffer segment
  213.  
  214.           movzx   di,[DMAhalf]
  215.           shl     di,1
  216.           mov     di,[dmarealbufsize+di]                ; start offset in DMAbuffer
  217.  
  218.           mov     al,1
  219.           sub     al,[DMAhalf]                     ; change DMAhalf : DMAhalf=1-DMAhalf
  220.           mov     [DMAhalf],al
  221.  
  222.           movzx   si,[DMAhalf]
  223.           shl     si,1
  224.           mov     si,[dmarealbufsize+si]           ; start offset in DMAbuffer
  225.           add     si,[dmarealbufsize+2]
  226.           mov     al,es:[si-1]
  227.           sub     si,[dmarealbufsize+2]
  228.  
  229.           rep stosb                                ; fill dmabuffer
  230.  
  231.           jmp     endoffill
  232.  
  233. Song_ends:; MOD ends here - clear DMAbuffer
  234.           les     di,[DMAbuffer]
  235.           movzx   di,[DMAhalf]
  236.           shl     di,1
  237.           mov     di,[dmarealbufsize+di]
  238.           mov     cx,[dmarealBufsize+2]
  239.           shr     cx,1
  240.           xor     ax,ax
  241.           rep stosw
  242.  
  243.           mov     al,1
  244.           sub     al,[DMAhalf]                     ; change DMAhalf : DMAhalf=1-DMAhalf
  245.           mov     [DMAhalf],al
  246.  
  247.           jmp     outside
  248. fill_8bit ENDP
  249.  
  250. SAVE_MAPPING PROC NEAR
  251.              mov      [errorsav],1
  252.              mov      dx,[savhandle]
  253.              mov      ah,47h
  254.              int      67h
  255.              cmp      ah,0
  256.              jne      endofsave
  257.              mov      [errorsav],0
  258. endofsave:   ret
  259. SAVE_MAPPING ENDP
  260.  
  261. RESTORE_MAPPING PROC NEAR
  262.                 cmp     [errorsav],1
  263.                 je      endofrestore
  264.                 mov     dx,[savHandle]
  265.                 mov     ah,48h
  266.                 int     67h
  267. endofrestore:   ret
  268. RESTORE_MAPPING ENDP
  269.  
  270. ends
  271. end